<?php
namespace Book\Controller;

use Base\Controller\AdminBaseController;

class BookCodeController extends AdminBaseController {

	protected $bookCodeModel;
    protected $bookModel;

    public function _initialize() {
        parent::_initialize();

        $this->bookModel = D("Book/Book");
        $this->bookCodeModel = D("Book/BookCode");
    }
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$bookCodeJson = file_get_contents('php://input');
		$bookCode = json_decode($bookCodeJson, true);
		
		// setting
		$bookCode['status'] = 1;
        
		// 执行添加
		$result = $this->bookCodeModel->add($bookCode);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}
        
		$newSeason = $this->bookCodeModel->where(array("id" => $result))->find();
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newSeason;
		$this->ajaxReturn($data);
	}
	
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$bookCode = $this->bookCodeModel->where(array("id" => $id))->find();
		if (!$bookCode) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $bookCode;
	    $this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->bookCodeModel->select();
// 		dump($result);
		
	    // return
	    $this->ajaxReturn($result);
	}

	/** 根据bid查看列表  */
	public function dataListByBid() {
		$bid = $_GET['bid'];
		
		$_GET['equalMap'] = array('bid' => $bid);
		$_GET['notEqualMap'] = array('status' => -1);
		$dataList = $this->bookCodeModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$bookCodeJson = file_get_contents('php://input');
		$bookCode = json_decode($bookCodeJson, true);
		
		// 执行更新
		$result = $this->bookCodeModel->where(array("id" => $id))->save($bookCode);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedModel = $this->bookCodeModel->where(array("id" => $id))->find();
			
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedModel;
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$bookCode = $this->bookCodeModel->where(array("id" => $id))->find();
		if (!$bookCode) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 判断该书是否存在借阅记录
		$borrow = M('Borrow')->where("book_code_id = $id and status < 2 order by id desc")->find();
		if ($borrow) {
			$data['code'] = 499;
			$data['msg'] = '，删除失败，该书存在借阅记录！';
			$this->ajaxReturn($data);
		}
        
		if (!$this->bookCodeModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index
    public function index() {
		$this->display();
		return;
	} */
	
	/** genCode */
    public function genCode() {
		$this->display(':code-gen');
		return;
	}

}

